package com.keya.pull;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keya.mapper.PullStudyRecordMapper;
import com.keya.selector.SelectorResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 拉取院方数据记录 服务实现类
 * </p>
 *
 * @author hsq
 * @since 2022-08-12
 */
@Service
public class PullStudyRecordService extends ServiceImpl<PullStudyRecordMapper, PullStudyRecord> {

    @Resource
    PullStudyRecordMapper pullStudyRecordMapper;

    public IPage<PullStudyRecordBO> page(String selector, Integer offset, Integer limit) throws Exception {
        QueryWrapper<PullStudyRecord> queryWrapper = getWrapperForListBySelector(selector);
        List<PullStudyRecordBO> list = pullStudyRecordMapper.getList(limit, offset, queryWrapper);
        int count = pullStudyRecordMapper.count(limit, offset, queryWrapper);
        IPage<PullStudyRecordBO> page = new Page<>(offset / limit + 1, limit);
        page.setRecords(list);
        page.setTotal(count);
        return page;
    }

    public QueryWrapper<PullStudyRecord> getWrapperForListBySelector(String selector) throws Exception {
        Map<String, String> mapperMap = new HashMap<>();
        mapperMap.put("study_time", "a.study_time");
        mapperMap.put("aetitle", "b.aetitle");
        mapperMap.put("status", "a.status");
        return SelectorResolver.getQueryWrapper(selector, mapperMap);
    }

}
